package socketEnv;

import com.supermap.desktop.core.utilties.FileUtilities;
import com.supermap.desktop.core.utilties.SystemPropertyUtilities;

import java.io.File;
import java.net.InetAddress;

/**
 * Created by hanyz on 2016/9/8.
 */
public class DataUtil {
	public static String serverIP = "192.168.15.249";
	//测试数据路径，只能只读打开
	public static String testDataPath = "\\\\192.168.15.249\\TestData\\GUIData\\900\\";
	public static String localDataPath = System.getProperty("user.dir") + "\\GUIData\\900\\";
	public static String remoteDataPath = "\\GUIData\\900\\";
	public static String resultPicturePath = "..\\..\\GUIPicture\\900\\";
	public static String expectPicturePath = "\\GUIPicture\\900\\";
	//测试结果文件路径，用于对比
	public static String resultPath_exp = "\\\\192.168.15.249\\TestData\\GUIData\\900\\out\\exp\\";
	//测试结果文件路径，存放测试结果文件，不上传
	public static String resultPath_actual = System.getProperty("user.dir") + "\\GUIData\\900\\out\\actual\\";

	public static String iDesktopTitle = "SuperMap iDesktop Cross 9D)";

	public static void inti() {
		//linux将共享文件挂在到/mnt/testdata。
		// 当前Ubuntu的挂在方式是:将mount -t cifs命令写到root home的.bashrc文件中随启动自动挂在。
		// 如果遇到启动机器后没有联网的问题，会挂在失败。需要手动挂载可能。
		if (SystemPropertyUtilities.isLinux()) {
			testDataPath = "\\mnt\\testdata\\GUIData\\900\\";
			resultPath_exp = "\\mnt\\testdata\\GUIData\\900\\out\\exp\\";
		}
	}

	public static String getIDesktopTitle() {
		try {
			iDesktopTitle = CrossProperties.getProperty(CrossProperties.PropertiesEnum.Core, "String_MessageBox_Title");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return iDesktopTitle;
	}

	public static void ensureReachable() throws Exception {
		ensureReachable(serverIP);
	}

	public static void ensureReachable(String ip) throws Exception {
		if (!isReachable(ip)) {
			Runtime.getRuntime().exec("route delete  192.168.15.249  mask 255.255.255.255 192.168.15.1");
			Runtime.getRuntime().exec("route add  192.168.15.249  mask 255.255.255.255 192.168.15.1");
		}
		if (!isReachable(ip)) {
			throw new Exception("ipAddr inaccessible");
		}
	}

	public static boolean isReachable(String remoteInetAddr) {
		boolean reachable = false;
		try {
			InetAddress address = InetAddress.getByName(remoteInetAddr);
			reachable = address.isReachable(5000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return reachable;
	}

	/**
	 * 从testDataPath路径下拷贝文件到GUIData，拷贝形同的文件结构路径
	 * @param dataPath 不需要加testDataPath前缀，方便在方法中拼接remote和local路径，例："DataConversion/testSQLQuery";
	 * @return 文件是否拷贝成功
	 */
	public static boolean copyData(String dataPath) throws Exception {
		ensureReachable();
		String remotePath = testDataPath + dataPath;
		String destinationPath = localDataPath + dataPath;
		boolean copyResult = false;
		if (new File(remotePath).isDirectory()) {
			copyResult = FileUtilities.copyDirectory(remotePath, destinationPath, true);
		} else {
			copyResult = FileUtilities.copyFile(remotePath, destinationPath, true);
		}
		if (!copyResult) {
			throw new Exception("文件：" + dataPath + "复制失败！\n"
					+ "remotePath:" + remotePath + "\n"
					+ "destinationPath:" + destinationPath + "\n");
		}
		return copyResult;
	}

	/**
	 * 拷贝文件型数据源
	 * @param dataPath
	 * @throws Exception
	 */
	public static Boolean copyUDB(String dataPath) throws Exception {
		String extensionName = dataPath.substring(dataPath.lastIndexOf("."), dataPath.length());
		String theOtherExtensionName = null;
		switch (extensionName) {
			case ".udb":
				theOtherExtensionName = ".udd";
				break;
			case ".udd":
				theOtherExtensionName = ".udb";
				break;
			case ".UDB":
				theOtherExtensionName = ".UDD";
				break;
			case ".UDD":
				theOtherExtensionName = ".UDB";
				break;
			default:
				break;
		}
		String theOtherDataPath = dataPath.replaceAll(extensionName, theOtherExtensionName);
		return copyData(dataPath) && copyData(theOtherDataPath);
	}

	/**
	 * 拷贝文件型数据源,并返回本地文件型数据源路径
	 * @param udbPath
	 * @throws Exception
	 */
	public static String udbPath(String udbPath) throws Exception {
		copyUDB(udbPath);
		return localDataPath + udbPath;
	}


	/**
	 * 拷贝文件型工作空间
	 * @param smwuPath
	 * @param ubdPath
	 * @throws Exception
	 */
	public static void copySMWU(String smwuPath,String ubdPath) throws Exception {
		if(copyData(smwuPath) && copyUDB(ubdPath))
			System.out.println("工作空间拷贝成功！");
	}
}
